package com.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

import android.util.Log;

import com.diana.MainActivity;
import com.diana.R;

/***************************** IMPORT *****************************/

public class WriteDictionnary {

	/************ DECLARATIONS ***************/
	private Dictionary dico;
	private MainActivity mainActivity;
	
	
	/************ CONSTRUCTOR ****************/
	
	public WriteDictionnary(Dictionary dico, MainActivity mActivity){
		this.dico = dico;
		this.mainActivity = mActivity;
		try {
			FromTxtCreateDictionary();
		} catch (IOException e) {
			e.printStackTrace();
		}
//	if(this.dico.getMap().get("hello").getToken() == null){
		
		

	}

	/************** METHODES *****************/
	public void FromTxtCreateDictionary()  throws java.io.IOException{

        //Variable of chronometer
        int i=0;
        Long stop = (long) 0;
        Long start = System.currentTimeMillis();
        int l=0;
        String mLine;
        String separator = mainActivity.getString(R.string.wordType_separator); 
        byte[] word;
        byte[] grammar;
       
        BufferedReader reader =new BufferedReader(new InputStreamReader(this.mainActivity.getApplicationContext().getAssets().open("dictionary.txt")));
        mLine = reader.readLine();

	        Log.e("debut","0");
	        while (mLine != null) {
        	
	        	//Get the list cut by Carver Class 
	        	//wordFeatures = mainActivity.getCarver().setCompleteString(mLine, separator);
	        	
	        	l=mLine.indexOf("_");
	        	word = mLine.substring(0,l).getBytes();
	        	
	        	grammar = mLine.substring(l+1).getBytes();
	        	
	        	
	        	//Chronometre
	        	i++;
	        	if(i==10000){
	        		stop = System.currentTimeMillis();
	        		stop = stop - start;
	        		Log.e("10000 mot lu",stop.toString());
	        		Log.e(mLine.substring(0,l),word.toString());
	        		}
	        	if(i==100000){
	        		stop = System.currentTimeMillis();
	        		stop = stop - start;
	        		Log.e("100000 mot lu",stop.toString());}
	        	if(i==200000){
	        		stop = System.currentTimeMillis();
	        		stop = stop - start;
	        		Log.e("200000 mot lu",stop.toString());}
	        	
	        	
	        	//Add word, wordType, token in the dictionary
	        	this.dico.addWordsInMap(word,grammar);
	        	
	        	
	        	//Read the next line
	            mLine = reader.readLine();
	        }
	        reader.close();
        	
	        stop = System.currentTimeMillis();
    		stop = stop - start;
    		Log.e("Fin",stop.toString());
    		
    		
    		mLine = "interlay";
    		word= mLine.getBytes();
    		
    		//FAire des thread pour reduire le temps !
    		//Cette ligne fait buguer il faut regarder comment le mot est ecrit en bytes dans la map
    	    Log.e("essai",this.dico.getMap().get(word).toString());
    		
	}   

		
		 
		  
	/************* ACCESSORS *************/
	
}
	

